package com.softa.quartz.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.softa.annotation.ServiceLog;
import com.softa.bean.DataGridModel;
import com.softa.dao.model.SysJob;
import com.softa.dao.quartz.SysJobMapper;
import com.softa.quartz.service.SysJobService;
import com.softa.utils.SpringUtils;
import org.springframework.stereotype.Service;

import com.softa.utils.DateUtils;

@Service
public class SysJobServiceImpl implements SysJobService {
	
	@Resource
	private SysJobMapper sysJobMapper;

	@Override
	@ServiceLog("添加定时任务")
	public String insert(SysJob record) {
		if(SpringUtils.containsBean(record.getBeanName())){
			record.setCreatetime(DateUtils.getyyyyMMddHHmmsssplit(new Date()));
			sysJobMapper.insert(record);
			return "录入成功";
		}else{
			return "1";//bean不存在
		}
		
	}

	@Override
	@ServiceLog("更新定时任务")
	public String updateByPrimaryKey(SysJob record) {
		if(SpringUtils.containsBean(record.getBeanName())){
			sysJobMapper.updateByPrimaryKey(record);
			return "修改成功";
		}else{
			return "1";//bean不存在
		}
	}

	@Override
	public Map<String, Object> querySysJobList(DataGridModel dataGrid) {
		Map<String, Object> map=new HashMap<String, Object>();
		List<SysJob> list=sysJobMapper.querySysJobList(dataGrid);
		for(SysJob job:list){
			String endtime=job.getEndtime();
			String nowtime=DateUtils.getyyyyMMddHHmmsssplit(new Date());
			if(endtime!=null&&!endtime.equals("")){
				if(nowtime.compareTo(endtime)>0){
					job.setState(2);//停止
					this.updateByPrimaryKey(job);
				}
			}
		}
		map.put("rows", list);
		map.put("total", list.size());
		return map;
	}

}
